<?php

include("header.php");

// Get info submitted through form
$nrow = $_REQUEST["nrow"];
$ncol = $_REQUEST["ncol"];
$nprob = $nrow*$ncol;
$start_time = $_REQUEST["start_time"];
$end_time = time();
$time_diff = $end_time - $start_time;
$minutes = floor($time_diff/60.0);
$seconds = $time_diff - $minutes*60.0;


// Get HTML for table printing problems and 
// indicating incorrect answers
$sol_tab = MakeSolutionTable();

// Record test results in database
RecordTest();

// Print summary of results

if($nprob==$ncorrect){
	$img = sprintf("images/elephant%02d.jpg", rand(1,11));
	$mess = "Great job! You've got the memory of an ELEPHANT!";
}else{
	$img = sprintf("images/monkey%02d.jpg", rand(1,5));
	$mess = "Aww... quit monkeying around. You can do better!";
}
?>

<center>
<p>
<table border="1" cellpadding="20">
	<tr>
		<td>
			<center><h1>Test Results</h1></center>
			
			<p><center><font size="+1"><b><i><?=$mess?></font></i></b></center></p>

			Total number of problems: <?=$nprob?><br>
			Total correct: <?=$ncorrect?> (<?=floor(100.0*$ncorrect/$nprob)?>%)<br>
			Total incorrect: <?=$nprob-$ncorrect?> (<?=floor(100.0*($nprob-$ncorrect)/$nprob)?>%)<br>
			Start Time: <?=date("r", $start_time)?><br>
			End Time: <?=date("r", $end_time)?><br>
			Time taken: <?=$minutes?>m <?=$seconds?>s<br>
		</td>
		<td>
			<img src="<?=$img?>" width="600">
		</td>
	</tr>
</table>
</p>
</center>

<hr width="85%">

<center>
<?=$sol_tab?>
</center>

<?php


include("footer.php");

//--------------------
// MakeSolutionTable
//--------------------
function MakeSolutionTable()
{
	global $nrow, $ncol, $nprob;
	global $ncorrect;
	
	$ret_str = "<table cellpadding='20'>\n";

	// Loop over problems
	$ncorrect = 0;
	for($j=0; $j<$nrow; $j++){
		$ret_str .= "<tr>\n";
		for($i=0; $i<$ncol; $i++){
			// Get problem from form info
			$prob = $i+$j*$ncol+1;
			$num1str = "q${prob}num1";
			$num2str = "q${prob}num2";
			
			$num1 = $_REQUEST[$num1str];
			$num2 = $_REQUEST[$num2str];

			$correct_answer = $num1*$num2;
			$user_answer = $_REQUEST["answer${prob}"];

			$ret_str .= "	<td align='right'>\n";			
			$ret_str .= get_problem_html($num1, "times", $num2);
			if(strlen($user_answer)==0 || $correct_answer!=$user_answer){
				$ret_str .= "<font color='red'>$correct_answer</font>\n";
			}else{
				$ncorrect++;
			}
			if(strlen($user_answer)!=0)$ret_str .= get_number_html($user_answer);
			$ret_str .= "	</td>\n";
		}
		$ret_str .= "</tr>\n";
	}
	
	$ret_str .= "<tr><td colspan=\"$ncol\"><center><form method='POST' action='test.php'><input type=\"submit\" value=\"Take Another Test!\"></form></center></td></tr>\n";
	$ret_str .= "</table>";
	
	return $ret_str;
}
?>
